package cn.iocoder.yudao.module.social.api.container;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.social.api.container.dto.ContainerReqDTO;
import cn.iocoder.yudao.module.social.api.container.dto.ContainerRespDTO;
import cn.iocoder.yudao.module.social.convert.container.ContainerConvert;
import cn.iocoder.yudao.module.social.dal.dataobject.container.ContainerDO;
import cn.iocoder.yudao.module.social.service.container.ContainerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.social.enums.ErrorCodeConstants.CONTAINER_NOT_EXISTS;

/**
 * 容器 API 实现类
 *
 * @author 芋道源码
 */
@RestController
@Validated
@Slf4j
public class ContainerApiImpl implements ContainerApi {

    @Resource
    private ContainerService containerService;

    @Override
    public CommonResult<Long> createContainer(ContainerReqDTO reqDTO) {
        try {
            Long id = containerService.createContainer(ContainerConvert.INSTANCE.convertToCreateReqVO(reqDTO));
            return success(id);
        } catch (Exception ex) {
            log.error("[createContainer][reqDTO({}) 创建容器异常]", reqDTO, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<Boolean> updateContainer(ContainerReqDTO reqDTO) {
        try {
            containerService.updateContainer(ContainerConvert.INSTANCE.convertToUpdateReqVO(reqDTO));
            return success(true);
        } catch (Exception ex) {
            log.error("[updateContainer][reqDTO({}) 更新容器异常]", reqDTO, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<Boolean> deleteContainer(Long id) {
        try {
            containerService.deleteContainer(id);
            return success(true);
        } catch (Exception ex) {
            log.error("[deleteContainer][id({}) 删除容器异常]", id, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<ContainerRespDTO> getContainer(Long id) {
        try {
            ContainerDO container = containerService.getContainer(id);
            if (container == null) {
                return CommonResult.error(CONTAINER_NOT_EXISTS);
            }
            return success(ContainerConvert.INSTANCE.convertApi(container));
        } catch (Exception ex) {
            log.error("[getContainer][id({}) 查询容器异常]", id, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<List<ContainerRespDTO>> getContainerList(Collection<Long> ids) {
        try {
            List<ContainerDO> containers = containerService.getContainerList(ids);
            return success(ContainerConvert.INSTANCE.convertApiList(containers));
        } catch (Exception ex) {
            log.error("[getContainerList][ids({}) 批量查询容器异常]", ids, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<List<ContainerRespDTO>> getContainerListByType(String type) {
        try {
            List<ContainerDO> containers = containerService.getContainerListByType(type);
            return success(ContainerConvert.INSTANCE.convertApiList(containers));
        } catch (Exception ex) {
            log.error("[getContainerListByType][type({}) 根据类型查询容器异常]", type, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

    @Override
    public CommonResult<List<ContainerRespDTO>> getContainerListByParent(Long parentId) {
        try {
            List<ContainerDO> containers = containerService.getContainerListByParent(parentId);
            return success(ContainerConvert.INSTANCE.convertApiList(containers));
        } catch (Exception ex) {
            log.error("[getContainerListByParent][parentId({}) 根据父容器查询容器异常]", parentId, ex);
            return CommonResult.error(500, "系统异常");
        }
    }

}
